package com.winner.crmsoftware.updatelocation;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.location.Location;
import android.os.AsyncTask;
import android.util.Log;

import com.winner.srmsoftware.json.JSONParser;
import com.winner.srmsoftware.json.ServiceHandler;

public class UpdateLocation {
	ServiceHandler jsonParser = new ServiceHandler();
	public static final String URL_UPDATE_LOCATION = "http://103.28.37.16:8080/crm_software/update_locaton_employee.php";
	String name;
	double latitude;
	double longitude;
	
	String employee_id;
	
	

	public UpdateLocation(String employee_id) {
		this.employee_id = employee_id;
	}

	public void updateLocation(Location currentLocation) {
		// If the location is valid
		if (currentLocation != null) {
			// Return the latitude and longitude as strings
			name = "Update Location";
			latitude = currentLocation.getLatitude();
			longitude = currentLocation.getLongitude();
			new AddNewCustomer().execute();

		}

	}

	class AddNewCustomer extends AsyncTask<String, String, String> {

		protected String doInBackground(String... args) {

			// Building Parameters
			// Note that create product url accepts POST method
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("name", name));
			params.add(new BasicNameValuePair("employee_id", employee_id));
			params.add(new BasicNameValuePair("latitude", String.valueOf(latitude)));
			params.add(new BasicNameValuePair("longitude", String.valueOf(longitude)));
			
			Log.e("parameters","name: " + name + ", employee_id: " + employee_id + ", latitude: " + latitude + ", longitude: " + longitude);

			jsonParser.makeServiceCall(URL_UPDATE_LOCATION,ServiceHandler.POST, params);

			return null;
		}
	}
}
